﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.Win32;
using Core;
using Microsoft.SqlServer.Server;
using Core.DataProvider;
using System.Data;

namespace Service.Agent
{
    class Helper
    {
        internal static bool IsWindowsCluster
        {
            get
            {
                bool isWindowsCluster = false;
                try
                {
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey("Cluster");
                    if (rk != null)
                        isWindowsCluster = true;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.ToString());
                    Logging.LogError(ex.ToString());
                }
                return isWindowsCluster;
            }
        }

        internal static string Fqdn
        {
            get
            {
                IPHostEntry host = Dns.GetHostEntry(Environment.MachineName);
                return host.HostName;
            }
        }

        internal static string WindowsClusterName
        {
            get
            {
                string windowsClusterName = string.Empty;
                try
                {
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey("Cluster");
                    if (rk != null)
                        windowsClusterName = rk.GetValue("ClusterName").ToString();
                }
                catch (Exception ex)
                {
                    Logging.LogError(ex.ToString());
                    //Console.WriteLine(ex.ToString());
                }
                return windowsClusterName;
            }
        }

        internal static void CheckAlertRule(EntityProperty entityProperty)
        {
            var q = from ar in MainService.AlertRuleList where ar.EntityTypePropertyId == entityProperty.EntityTypeProperty.Id select ar;

            foreach (AlertRule ar in q)
            {
                bool isAlert = false;
                switch (entityProperty.EntityTypeProperty.TypeValue)
                {
                    case 0:                 // string
                        Logging.LogInformation("Alertrule matched");
                        if (ar.StringWarningValue != null)
                        {
                            switch (ar.StringWarningType)
                            {
                                case 0:         // string.equal
                                    if (entityProperty.StringValue.Equals(ar.StringWarningValue, StringComparison.InvariantCultureIgnoreCase))
                                        isAlert = true;
                                    break;
                                case 1:         // !string.equal
                                    if (!entityProperty.StringValue.Equals(ar.StringWarningValue, StringComparison.InvariantCultureIgnoreCase))
                                        isAlert = true;
                                    break;
                                case 2:         // contains
                                    Logging.LogInformation("!!!!!! String warningtype found....");
                                    if(entityProperty.StringValue.ToLowerInvariant().Contains(ar.StringWarningValue.ToLowerInvariant()))
                                        isAlert = true;
                                    break;
                            }
                        }
                        break;
                    case 1:                 // byte, tinyint, int
                        break;
                    case 2:                 // datetime
                        break;
                    case 3:                 // double
                        break;
                    case 4:                 // bigint
                        break;
                }
                if(isAlert)
                {
                    Logging.LogInformation("!!!!! Alert found - " + entityProperty.Entity.Name + " (" + entityProperty.EntityTypeProperty.Name + "). Value: " + entityProperty.StringValue);
                    ForwarderCommunication.AddAlert(ar, entityProperty.Entity, entityProperty);
                }
            }
        }
    }
}
